package org.jeecg.modules.yw.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.modules.yw.entity.MaterialInfo;
import org.jeecg.modules.yw.result.Result;
import org.jeecg.modules.yw.service.MaterialInfoService;
import org.jeecg.modules.yw.utils.CommonUtils;
import org.jeecg.modules.yw.utils.DESUtil;
import org.jeecg.modules.yw.utils.DateUtils;
import org.jeecg.modules.yw.utils.UserUtil;
import org.jeecg.modules.yw.vo.MaterialInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@Api(tags = "品类费用管理")
@RequestMapping("/app/materialInfo")
public class MaterialInfoController {
    @Resource
    private MaterialInfoService materialInfoService;
    @Autowired
    private ISysBaseAPI sysBaseAPI;
    @ApiOperation(value = "获取品类费用列表")
    @GetMapping("/page")
    public  org.jeecg.common.api.vo.Result page(Page<MaterialInfo> page,
                                                @RequestParam(value = "name", required = false) String name,
                                                @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize ) {
        page.setCurrent(pageNo);
        page.setSize(pageSize);
        QueryWrapper<MaterialInfo> queryWrapper = new QueryWrapper<>();
        if (name != null) {
            queryWrapper.like("category", name);
        }
        queryWrapper.orderByAsc("morder");
        IPage<MaterialInfo> result = materialInfoService.page(page, queryWrapper);
        return  org.jeecg.common.api.vo.Result.ok(result);
    }
    @ApiOperation(value = "添加品类费用")
    @PostMapping("/addMaterialInfo")
    public Result addMaterialInfo(@RequestBody MaterialInfoVo articleVo, HttpServletRequest request) throws Exception {
        MaterialInfo article = new MaterialInfo();
        BeanUtils.copyProperties(articleVo, article);
        article.setId(CommonUtils.getID());
        article.setCreatetime(DateUtils.getCurrentDate());
        article.setCreator(UserUtil.getUserId(request,sysBaseAPI));
        materialInfoService.save(article);
        return Result.success();
    }
    @ApiOperation(value = "修改品类费用")
    @PostMapping("/editMaterialInfo")
    public Result editMaterialInfo(@RequestBody MaterialInfoVo articleVo) throws Exception {
        MaterialInfo article = new MaterialInfo();
        BeanUtils.copyProperties(articleVo, article);
        materialInfoService.updateById(article);
        return Result.success();
    }
    @ApiOperation(value = "删除品类费用")
    @DeleteMapping("/deleteMaterialInfo")
    public  org.jeecg.common.api.vo.Result deleteMaterialInfo(@RequestParam(value = "id") String id) {
        materialInfoService.removeById(id);
        return  org.jeecg.common.api.vo.Result.ok();
    }
}
